/**
 * 
 */
package com.goldentrail.neo4j;

import java.net.URI;
import java.net.URISyntaxException;
import java.util.ArrayList;

import javax.ws.rs.core.MediaType;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.ClientHandlerException;
import com.sun.jersey.api.client.ClientResponse;
import com.sun.jersey.api.client.UniformInterfaceException;
import com.sun.jersey.api.client.WebResource;

/**
 * Java Class to interact with nodes in a remote Neo4j database encapsulating
 * REST calls to the server. Use createNode() to create a new node in the db, or
 * one of the getNode()/getNodes() functions to interact with an existing node
 * 
 * @author mikeagun
 */
public class Neo4jNode {
	private static final boolean verboseMode = false;
	private URI nodeUri;

	/**
	 * Creates a new node object connected to the specified URI. Note that no
	 * error checking is done in this constructor, use one of the getNode
	 * functions for error checking.
	 * 
	 * @param nodeUri
	 *            URI of the node to associate new object with
	 */
	public Neo4jNode(URI nodeUri) {
		this.nodeUri = nodeUri;
	}

	/**
	 * Creates a new node object connected to the specified URI. Note that no
	 * error checking is done in this constructor, use one of the getNode
	 * functions for error checking.
	 * 
	 * @param nodeUri
	 *            URI of the node to associate new object with
	 */
	public Neo4jNode(String nodeUri) {
		try {
			this.nodeUri = new URI(nodeUri);
		} catch (URISyntaxException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	/**
	 * Creates a Node in the database.
	 * 
	 * @return new node added to server Neo4j database
	 */
	public static Neo4jNode createNode() {
		WebResource resource = Client.create().resource(
				URIs.NODE_ENTRY_POINT_URI);
		ClientResponse response = resource.accept(MediaType.APPLICATION_JSON)
				.type(MediaType.APPLICATION_JSON).entity("{}"/* Empty Node */)
				.post(ClientResponse.class); // <<tt>POST</tt> [] to the node
												// entry point URI
		if (verboseMode)
			System.out.println(String.format(
					"POST to [%s], status code [%d], location header [%s]",
					URIs.NODE_ENTRY_POINT_URI, response.getStatus(), response
							.getLocation().toString())); // response should be
															// 201
															// Created

		return new Neo4jNode(response.getLocation());
	}

	/**
	 * Creates a Node in the database and fills in properties on that node.
	 * 
	 * @param propertiesJson
	 *            JSON string of properties to give node
	 * @return new node added to server Neo4j database
	 */
	public static Neo4jNode createNode(String propertiesJson) {
		// System.out.println("In Neo4jNode.createNode(String):Start");
		WebResource resource = Client.create().resource(
				URIs.NODE_ENTRY_POINT_URI);
		// System.out.println("In Neo4jNode.createNode():Middle");
		ClientResponse response = resource.accept(MediaType.APPLICATION_JSON)
				.type(MediaType.APPLICATION_JSON).entity(propertiesJson)
				.post(ClientResponse.class); // <<tt>POST</tt> [] to the node
												// entry point URI
		if (verboseMode)
			System.out
					.println(String
							.format("POST to [%s] with [%s], status code [%d], location header [%s]",
									URIs.NODE_ENTRY_POINT_URI, propertiesJson,
									response.getStatus(), response
											.getLocation().toString())); // response
																			// should
																			// be
																			// 201
																			// Created
		// System.out.println("In Neo4jNode.createNode(String):Finished");
		return new Neo4jNode(response.getLocation());
	}

	/**
	 * gets the string representation of the JSON Object containing all the
	 * node's properties
	 * 
	 * @return String containing JSON Object of properties
	 */
	public String getPropertiesJSON() {
		String propertyUri = nodeUri.toString() + "/properties";
		WebResource resource = Client.create().resource(propertyUri);
		ClientResponse response = resource.accept(MediaType.APPLICATION_JSON)
				.type(MediaType.APPLICATION_JSON).get(ClientResponse.class);
		// response should be 200 OK i think
		if (verboseMode)
			System.out.println(String.format(
					"GET to [%s], status code [%d], returned data: ",
					propertyUri, response.getStatus()));
		String resp = null;
		try {
			resp = response.getEntity(String.class);
		} catch (Exception ex) {
			ex.printStackTrace();
			return null;
		}
		if (verboseMode)
			System.out.println(resp);
		return resp;
	}

	/**
	 * Retrieves the Node with the specified Neo4j id from the database. only
	 * use if you know the Neo4j assigned id of a node
	 * 
	 * @param index
	 *            index of the node to be retrieved
	 * @return node object retrieved from database. null if not found
	 */
	public static Neo4jNode getNode(long index) {
		WebResource resource = Client.create().resource(
				URIs.NODE_ENTRY_POINT_URI + "/" + index);
		// The get is to ensure that the node actually exists
		ClientResponse response = resource.accept(MediaType.APPLICATION_JSON)
				.type(MediaType.APPLICATION_JSON).get(ClientResponse.class);
		// response should be 200
		if (verboseMode)
			System.out.println(String.format(
					"GET to [%s], status code [%d], returned data: ",
					URIs.NODE_ENTRY_POINT_URI + "/" + index,
					response.getStatus()));

		String resp = null;
		try {
			resp = response.getEntity(String.class);
		} catch (Exception ex) {
			ex.printStackTrace();
			return null;
		}
		if (verboseMode)
			System.out.println(resp);
		if (response.getStatus() != 200)
			return null;
		try {
			JSONObject json = new JSONObject(resp);
			return new Neo4jNode(json.getString("self"));
		} catch (ClientHandlerException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (UniformInterfaceException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * Retrieves the Node with the specified URI from the database.
	 * 
	 * @param nodeUri
	 *            URI of the node to be retrieved
	 * @return node object retrieved from database. null if not found
	 */
	public static Neo4jNode getNode(String nodeUri) {
		WebResource resource = Client.create().resource(nodeUri);
		// the get is to ensure that the node actually exists
		ClientResponse response = resource.accept(MediaType.APPLICATION_JSON)
				.type(MediaType.APPLICATION_JSON).get(ClientResponse.class);
		// response should be 200
		if (verboseMode)
			System.out.println(String.format(
					"GET to [%s], status code [%d], returned data: ", nodeUri,
					response.getStatus()));

		String resp = null;
		try {
			resp = response.getEntity(String.class);
		} catch (Exception ex) {
			ex.printStackTrace();
			return null;
		}
		if (verboseMode)
			System.out.println(resp);
		if (response.getStatus() != 200)
			return null;
		try {
			JSONObject json = new JSONObject(resp);
			return new Neo4jNode(json.getString("self"));
		} catch (ClientHandlerException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (UniformInterfaceException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * Retrieves the list of nodes in the specified index with the given
	 * key/value.
	 * 
	 * @param index
	 *            The index to search in
	 * @param key
	 *            the key of the nodes to find
	 * @param value
	 *            the key value to look for
	 * @return ArrayList of nodes retrieved from database
	 */
	public static ArrayList<Neo4jNode> getNodes(String index, String key,
			String value) {
		ArrayList<Neo4jNode> results = new ArrayList<Neo4jNode>();
		// currently no error checking, should check for invalid URL and
		// URL-encode spaces and such (space=%20)
		String searchUri = URIs.NODE_INDEX_URI + "/" + index + "/" + key + "/"
				+ value;
		WebResource resource = Client.create().resource(searchUri);
		ClientResponse response = resource.accept(MediaType.APPLICATION_JSON)
				.type(MediaType.APPLICATION_JSON).get(ClientResponse.class);
		// response should be 200 OK
		if (verboseMode)
			System.out.println(String.format(
					"GET to [%s], status code [%d], returned data: ",
					searchUri, response.getStatus()));
		String resp = null;
		try {
			resp = response.getEntity(String.class);
		} catch (Exception ex) {
			ex.printStackTrace();
			return null;
		}
		if (verboseMode)
			System.out.println(resp);
		if (response.getStatus() != 200)
			return null;

		try {
			JSONArray json = new JSONArray(resp);
			for (int i = 0; i < json.length(); i++) {
				JSONObject jsonObj = json.getJSONObject(i);
				results.add(new Neo4jNode(jsonObj.getString("self")));
			}
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return results;
	}

	/**
	 * Retrieves the first node found in the specified index with the given
	 * key/value.
	 * 
	 * @param index
	 *            The index to search in
	 * @param key
	 *            the key of the node to find
	 * @param value
	 *            the key value to look for
	 * @return ArrayList of nodes retrieved from database
	 */
	public static Neo4jNode getNode(String index, String key, String value) {
		// currently no error checking, should check for invalid URL and
		// URL-encode spaces and such (space=%20)
		String searchUri = URIs.NODE_INDEX_URI + "/" + index + "/" + key + "/"
				+ value;
		WebResource resource = Client.create().resource(searchUri);
		ClientResponse response = resource.accept(MediaType.APPLICATION_JSON)
				.type(MediaType.APPLICATION_JSON).get(ClientResponse.class);
		// response should be 200 OK
		if (verboseMode)
			System.out.println(String.format(
					"GET to [%s], status code [%d], returned data: ",
					searchUri, response.getStatus()));
		String resp = null;
		try {
			resp = response.getEntity(String.class);
		} catch (Exception ex) {
			ex.printStackTrace();
			return null;
		}
		if (verboseMode)
			System.out.println(resp);

		if (response.getStatus() != 200)
			return null;

		try {
			JSONArray json = new JSONArray(resp);
			if (json.length() > 0) {
				JSONObject jsonObj = json.getJSONObject(0);
				return new Neo4jNode(jsonObj.getString("self"));
			}
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return null;
	}

	/**
	 * Gets the URI of the Node.
	 * 
	 * @return URI of node
	 */
	public URI getURI() {
		return nodeUri;
	}

	/**
	 * Adds or sets a property on the Node.
	 * 
	 * @param propertyName
	 *            name of property to create / set
	 * @param propertyValue
	 *            value to give to property
	 */
	public void addProperty(String propertyName, String propertyValue) {
		String propertyUri = nodeUri.toString() + "/properties/" + propertyName;
		WebResource resource = Client.create().resource(propertyUri);
		ClientResponse response = resource.accept(MediaType.APPLICATION_JSON)
				.type(MediaType.APPLICATION_JSON)
				.entity("\"" + propertyValue + "\"").put(ClientResponse.class);
		// response should be 204 No Content
		if (verboseMode)
			System.out.println(String.format("PUT to [%s], status code [%d]",
					propertyUri, response.getStatus()));
	}

	/**
	 * Adds the node to the specified index with the given key/value. Note that
	 * multiple nodes can have the same key/value or one node can be entered
	 * more than once
	 * 
	 * @param index
	 *            index to add the node to
	 * @param key
	 *            key index the node under
	 * @param value
	 *            value to give the node
	 */
	public void indexNode(String index, String key, String value) {
		// currently no error checking, should check for invalid URL and
		// URL-encode spaces and such (space=%20)
		WebResource resource = Client.create().resource(
				URIs.NODE_INDEX_URI + "/" + index + "/" + key + "/" + value);
		ClientResponse response = resource.accept(MediaType.APPLICATION_JSON)
				.type(MediaType.APPLICATION_JSON).entity("\"" + nodeUri + "\"")
				.post(ClientResponse.class); // <<tt>POST</tt> [] to the node
												// entry point URI
		if (verboseMode)
			System.out.println(String.format(
					"POST to [%s], status code [%d], location header [%s]",
					URIs.NODE_ENTRY_POINT_URI, response.getStatus(), response
							.getLocation().toString())); // response should be
															// 201
															// Created
	}

	/**
	 * Gets the current value of the specified property on the node.
	 * 
	 * @param propertyName
	 *            Name of the property to retrieve
	 * @return value of the property
	 */
	public String getProperty(String propertyName) {
		String propertyUri = nodeUri.toString() + "/properties/" + propertyName;
		WebResource resource = Client.create().resource(propertyUri);
		ClientResponse response = resource.accept(MediaType.APPLICATION_JSON)
				.type(MediaType.APPLICATION_JSON).get(ClientResponse.class);
		// response should be 200 OK i think
		if (verboseMode)
			System.out.println(String.format(
					"GET to [%s], status code [%d], returned data: ",
					propertyUri, response.getStatus()));
		String resp = null;
		try {
			resp = response.getEntity(String.class);
		} catch (Exception ex) {
			ex.printStackTrace();
			return null;
		}
		if (verboseMode)
			System.out.println(resp);
		resp = resp.substring(1, resp.length() - 1);
		return resp;
	}

	/**
	 * Adds a relationship from this Node to the specified Node. Note that you
	 * can create two identical relationships accidentally
	 * 
	 * @param endNode
	 *            Node to relate this node to
	 * @param relationshipType
	 *            Relationship type
	 * @return Relationship that was just created
	 */
	public Relationship addRelationship(Neo4jNode endNode,
			String relationshipType) {
		JSONObject json = new JSONObject();
		try {
			json.put("to", endNode.nodeUri.toString());
			json.put("type", relationshipType);
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		String fromUri = nodeUri.toString() + "/relationships";

		WebResource resource = Client.create().resource(fromUri);
		ClientResponse response = resource.accept(MediaType.APPLICATION_JSON)
				.type(MediaType.APPLICATION_JSON).entity(json.toString())
				.post(ClientResponse.class);
		if (verboseMode)
			System.out.println(String.format(
					"POST to [%s], status code [%d], location header [%s]",
					fromUri, response.getStatus(), response.getLocation()));
		return new Relationship(response.getLocation());
	}

	/**
	 * Gets all the relationships on the node that match certain criteria
	 * 
	 * @param direction
	 *            in,out, or all
	 * @param types
	 *            types of relationships to return
	 * @return list of retrieved relationships
	 */
	public ArrayList<Relationship> getRelationships(String direction,
			String... types) {
		ArrayList<Relationship> relationships = new ArrayList<Relationship>();
		StringBuilder searchUri = new StringBuilder();
		searchUri.append(nodeUri);
		searchUri.append("/relationships/" + direction);
		if (types.length > 0) {
			searchUri.append("/" + types[0]);
			for (int i = 1; i < types.length; i++)
				searchUri.append("&" + types[i]);
		}
		WebResource resource = Client.create().resource(searchUri.toString());
		ClientResponse response = resource.accept(MediaType.APPLICATION_JSON)
				.type(MediaType.APPLICATION_JSON).get(ClientResponse.class);
		// response should be 200 OK
		if (verboseMode)
			System.out.println(String.format(
					"GET to [%s], status code [%d], returned data: ",
					searchUri, response.getStatus()));
		String resp = null;
		try {
			resp = response.getEntity(String.class);
		} catch (Exception ex) {
			ex.printStackTrace();
			return null;
		}
		if (verboseMode)
			System.out.println(resp);
		JSONArray json;
		try {
			json = new JSONArray(resp);
			for (int i = 0; i < json.length(); i++)
				relationships.add(new Relationship((json.getJSONObject(i)
						.getString("self"))));
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return relationships;
	}

	public String toString() {
		return nodeUri.toString();
	}

	/**
	 * Checks if another node object points to the same database node as this
	 * node object.
	 * 
	 * @param n
	 *            node object to check for equivalence
	 * @return true if the node objects point to the same database node, false
	 *         otherwise
	 */
	public boolean equals(Neo4jNode n) {
		if (n == null)
			return false;
		if (n.nodeUri == null)
			return false;
		return n.nodeUri.equals(nodeUri);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see java.lang.Object#equals(java.lang.Object)
	 */
	@Override
	public boolean equals(Object obj) {
		if (obj instanceof Neo4jNode)
			return this.equals((Neo4jNode) obj);
		else
			return false;
	}

	/**
	 * Traverses from this node using the given Traversal Specification, and
	 * returning a list of nodes
	 * 
	 * @param t
	 *            Traversal spec describing how to traverse and what nodes to
	 *            return
	 * @return list of nodes returned by the traversal's return filter
	 */
	public ArrayList<Neo4jNode> traverseNode(TraverserSpec t) {
		ArrayList<Neo4jNode> results = new ArrayList<Neo4jNode>();

		String searchUri = nodeUri.toString() + "/traverse/node";
		WebResource resource = Client.create().resource(searchUri);
		ClientResponse response = resource.accept(MediaType.APPLICATION_JSON)
				.type(MediaType.APPLICATION_JSON).entity(t.toString())
				.post(ClientResponse.class);
		if (verboseMode)
			System.out.println(t.toString());
		// response should be 200 OK
		if (verboseMode)
			System.out.println(String.format(
					"POST to [%s], status code [%d], returned data: ",
					searchUri, response.getStatus()));
		String resp = null;
		try {
			resp = response.getEntity(String.class);
		} catch (Exception ex) {
			ex.printStackTrace();
			return null;
		}
		if (verboseMode)
			System.out.println(resp);
		if (response.getStatus() != 200)
			return null;

		try {
			JSONArray json = new JSONArray(resp);
			for (int i = 0; i < json.length(); i++) {
				JSONObject jsonObj = json.getJSONObject(i);
				results.add(new Neo4jNode(jsonObj.getString("self")));
			}
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return results;

	}

	/**
	 * Traverses from this node using the given Traversal Specification, and
	 * returning a list of nodes. This function is used both to create a paged
	 * traversal and to page through the results
	 * 
	 * @param t
	 *            Traversal spec describing how to traverse and what nodes to
	 *            return
	 * @param URINext
	 *            should be a URI set to null to create the traversal, or a URI
	 *            set by this function to page through a traversal.
	 * @return next page of nodes returned by the traversal's return filter.
	 *         null if traverser is finished or on error
	 */
	public ArrayList<Neo4jNode> traverseNodePaged(TraverserSpec t, URI URINext) {
		ArrayList<Neo4jNode> results = new ArrayList<Neo4jNode>();
		WebResource resource = null;
		ClientResponse response = null;
		String searchUri = nodeUri.toString() + "/paged/traverse/node";
		if (URINext == null) {
			resource = Client.create().resource(searchUri);
			response = resource.accept(MediaType.APPLICATION_JSON)
					.type(MediaType.APPLICATION_JSON).entity(t.toString())
					.post(ClientResponse.class);
		} else {
			resource = Client.create().resource(URINext);
			response = resource.accept(MediaType.APPLICATION_JSON)
					.type(MediaType.APPLICATION_JSON).get(ClientResponse.class);
		}

		if (verboseMode)
			System.out.println(t.toString());
		// response should be 200 OK
		if (verboseMode) {
			if (URINext == null)
				System.out
						.println(String
								.format("POST to [%s], location: [%s], status code [%d], returned data: ",
										searchUri, response.getLocation(),
										response.getStatus()));
			else
				System.out.println(String.format(
						"POST to [%s], status code [%d], returned data: ",
						URINext, response.getStatus()));
		}
		String resp = null;
		try {
			resp = response.getEntity(String.class);
		} catch (Exception ex) {
			ex.printStackTrace();
			return null;
		}
		if (verboseMode)
			System.out.println(resp);

		if (response.getStatus() != 200 && response.getStatus() != 201) {
			URINext = null;
			return null;
		}

		if (URINext == null)
			URINext = response.getLocation();

		try {
			JSONArray json = new JSONArray(resp);
			for (int i = 0; i < json.length(); i++) {
				JSONObject jsonObj = json.getJSONObject(i);
				results.add(new Neo4jNode(jsonObj.getString("self")));
			}
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return results;

	}

	/**
	 * Traverses from this node using the given Traversal Specification, and
	 * returning a list of paths out from the node
	 * 
	 * @param t
	 *            Traversal spec describing how to traverse and what paths to
	 *            return
	 * @return list of paths returned by the traversal's return filter
	 */
	public ArrayList<PathResult> traversePath(TraverserSpec t) {
		ArrayList<PathResult> results = new ArrayList<PathResult>();

		String searchUri = nodeUri.toString() + "/traverse/path";
		WebResource resource = Client.create().resource(searchUri);
		ClientResponse response = resource.accept(MediaType.APPLICATION_JSON)
				.type(MediaType.APPLICATION_JSON).entity(t.toString())
				.post(ClientResponse.class);
		if (verboseMode)
			System.out.println(t.toString());
		// response should be 200 OK
		if (verboseMode)
			System.out.println(String.format(
					"POST to [%s], status code [%d], returned data: ",
					searchUri, response.getStatus()));
		String resp = null;
		try {
			resp = response.getEntity(String.class);
		} catch (Exception ex) {
			ex.printStackTrace();
			return null;
		}
		if (verboseMode)
			System.out.println(resp);
		if (response.getStatus() != 200)
			return null;

		try {
			JSONArray json = new JSONArray(resp);
			for (int i = 0; i < json.length(); i++) {
				JSONObject jsonObj = json.getJSONObject(i);
				results.add(PathResult.fromJson(jsonObj));
			}
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return results;

	}

	/**
	 * Finds all paths starting at this node that follow the given path
	 * specification
	 * 
	 * @param path
	 *            path specification to use. describes how to find paths and
	 *            where to end up
	 * @return list of paths that follow the given path specification.
	 */
	public ArrayList<PathResult> findPaths(PathSpec path) {
		ArrayList<PathResult> results = new ArrayList<PathResult>();
		String searchUri = nodeUri.toString() + "/paths";
		WebResource resource = Client.create().resource(searchUri);
		ClientResponse response = resource.accept(MediaType.APPLICATION_JSON)
				.type(MediaType.APPLICATION_JSON).entity(path.toString())
				.post(ClientResponse.class);
		if (verboseMode)
			System.out.println(path.toString());
		// response should be 200 OK
		if (verboseMode)
			System.out.println(String.format(
					"POST to [%s], status code [%d], returned data: ",
					searchUri, response.getStatus()));
		String resp = null;
		try {
			resp = response.getEntity(String.class);
		} catch (Exception ex) {
			ex.printStackTrace();
			return null;
		}
		if (verboseMode)
			System.out.println(resp);
		if (response.getStatus() != 200)
			return null;

		try {
			JSONArray json = new JSONArray(resp);
			for (int i = 0; i < json.length(); i++) {
				JSONObject jsonObj = json.getJSONObject(i);
				results.add(PathResult.fromJson(jsonObj));
			}
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return results;
	}

	/**
	 * Finds the first path starting at this node that follows the given path
	 * specification
	 * 
	 * @param path
	 *            path specification to use. describes how to find paths and
	 *            where to end up
	 * @return the first path that follows the given path specification.
	 */
	public PathResult findPath(PathSpec path) {
		PathResult result = null;
		String searchUri = nodeUri.toString() + "/path";
		WebResource resource = Client.create().resource(searchUri);
		ClientResponse response = resource.accept(MediaType.APPLICATION_JSON)
				.type(MediaType.APPLICATION_JSON).entity(path.toString())
				.post(ClientResponse.class);
		if (verboseMode)
			System.out.println(path.toString());
		// response should be 200 OK
		if (verboseMode)
			System.out.println(String.format(
					"POST to [%s], status code [%d], returned data: ",
					searchUri, response.getStatus()));
		String resp = null;
		try {
			resp = response.getEntity(String.class);
		} catch (Exception ex) {
			ex.printStackTrace();
			return null;
		}
		if (verboseMode)
			System.out.println(resp);
		if (response.getStatus() != 200)
			return null;

		try {
			JSONObject json = new JSONObject(resp);
			result = PathResult.fromJson(json);
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return result;
	}

}
